from utils.drivers.database.models.news import News, NewsModel
from utils.drivers.database.models.news_type import NewsType
from utils.drivers.database.models.params import Params, SiteInfoModel, SlideshowModel
from utils.drivers.database.models.product import Product, ProductModel
from utils.drivers.database.models.product_type import ProductType, ProductTypeModel
from utils.enums import Language, Sort, TimeKey


class G:
    pageSize = 12
    SiteInfoCN = None
    SiteInfoEN = None
    Slideshow = None
    ProductTypeList = None
    PushProduct = None
    NewsTypeList = None
    NewestNews = None

    @classmethod
    async def get_site_info(cls, language) -> SiteInfoModel:
        if not G.SiteInfoCN or not G.SiteInfoEN:
            G.SiteInfoCN, G.SiteInfoEN = await Params.get_site_info()

        if language == Language.CN:
            return G.SiteInfoCN
        elif language == Language.EN:
            return G.SiteInfoEN

    @classmethod
    async def get_slideshow(cls) -> list[SlideshowModel]:
        if not G.Slideshow:
            G.Slideshow = await Params.get_slideshow()
        return G.Slideshow

    @classmethod
    async def get_product_type_list(cls, language) -> list[ProductTypeModel]:
        if not G.ProductTypeList:
            G.ProductTypeList = await ProductType.get_all()

        return [pt.model(language) for pt in G.ProductTypeList]

    @classmethod
    async def get_push_product(cls, language) -> list[ProductModel]:
        if not G.PushProduct:
            G.PushProduct, _ = await Product.get(push=True, sort=(TimeKey.UpdateTime, Sort.Desc))

        return [p.model(language, await cls.get_product_type_list(language)) for p in G.PushProduct]

    @classmethod
    async def get_news_type_list(cls, language) -> list[dict]:
        if not G.NewsTypeList:
            G.NewsTypeList = await NewsType.get_all()

        return [nt.model(language) for nt in G.NewsTypeList]

    @classmethod
    async def get_newest_news(cls, language) -> list[NewsModel]:
        if not G.NewestNews:
            G.NewestNews, _ = await News.get(page_size=5, sort=(TimeKey.UpdateTime, Sort.Desc))

        return [n.model(language, await cls.get_news_type_list(language)) for n in G.NewestNews]
